<?php

namespace App\Models\Product;

use Illuminate\Database\Eloquent\Model;
use App\Models\Config as ConfigModel;

class Product extends Model
{

    protected $table = 'product';

    protected $dateFormat = 'U';

    const CREATED_AT = 'createtime';

    const UPDATED_AT = null;

    protected $guarded = [];

    protected $appends = [
        'thumbs_text', //产品单张图
        'thumbs_list', //产品多张图
    ];

    public function getThumbsTextAttribute()
    {

        $urls = ConfigModel::where('name', 'shourl')->value('value');

        $url = env('SITE_URL', $urls);

        $thumbs_text = '';

        $thumbs = !empty($this->attributes['thumbs']) ?  explode(',', $this->attributes['thumbs']) : [];

        if(count($thumbs) > 0)
        {
            $thumbs_text = $url.$thumbs[0];
        } else {
            $thumbs_text = $url. '/assets/images/product.jpg';
        }

        return $thumbs_text;
    }

    public function getThumbsListAttribute()
    {

        $urls = ConfigModel::where('name', 'shourl')->value('value');

        $url = env('SITE_URL', $urls);

        $thumbs_text = '';

        $thumbs = !empty($this->attributes['thumbs']) ?  $this->attributes['thumbs']: [];

        if(!empty($thumbs))
        {
            $thumbs = str_ireplace("/uploads/", $url. "/uploads/",$thumbs);
            $thumbs_text = explode(',', $thumbs);

        } else {
            $thumbs_text = [$url. '/assets/images/product.jpg'];
        }

        return $thumbs_text;
    }
}